package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.GatherLiveResponse
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ItemGatherLiveStationBinding
import com.gexiaobao.pigeon.ui.view.GridSpacingItemDecoration
import com.gexiaobao.pigeon.ui.view.recycleview.FullyGridLayoutManager

/**
 * @Author      : hxw
 * @Date        : 2024/8/22 15:54
 * @Describe    :
 */
class AdapterGatherLiveStation(data: ArrayList<GatherLiveResponse.GatherLiveBean>) :
    BaseQuickAdapter<GatherLiveResponse.GatherLiveBean, DataBindBaseViewHolder>(R.layout.item_gather_live_station, data) {

    private var mOnItemLiveClickListener: OnItemLiveClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: GatherLiveResponse.GatherLiveBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemGatherLiveStationBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        holder.itemView.apply {
            binding.swipeRecyclerView.adapter = null
            if (item.isEmpty() || item.channels.isEmpty()) {
                holder.setGone(R.id.swipeRecyclerView, true)//隐藏
                return
            }
            holder.setGone(R.id.swipeRecyclerView, false)//显示
            val mAdapter = GatherLiveListAdapter(item.channels)
            mAdapter.notifyDataSetChanged()
            binding.swipeRecyclerView.apply {
                layoutManager = GridLayoutManager(context, 2)
                binding.swipeRecyclerView.addItemDecoration(GridSpacingItemDecoration(2, 8, false))
                adapter = mAdapter
                mAdapter.notifyDataSetChanged()

                mAdapter.setOnItemClickListener(object : GatherLiveListAdapter.OnItemClickListener {
                    override fun onItemClick(position: Int, data: GatherLiveResponse.GatherLiveBean.ChannelListBean) {
                        mOnItemLiveClickListener?.onItemClick(data)
                    }
                })
            }
        }
    }

    fun setOnItemLiveClickListener(listener: OnItemLiveClickListener?) {
        mOnItemLiveClickListener = listener
    }

    interface OnItemLiveClickListener {
        fun onItemClick(data: GatherLiveResponse.GatherLiveBean.ChannelListBean)
    }
}